package com.chemical.search.view.head;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.chemical.search.dto.JmeDTO;
import com.chemical.search.model.BaseAttribute;
import com.chemical.search.model.StructureInfo;
import com.chemical.search.model.UserInfo;
import com.chemical.search.service.IBaseAttributeService;
import com.chemical.search.service.IStructureInfoService;
import com.chemical.search.util.page.Condition;
import com.chemical.search.util.page.Page;
import com.chemical.search.util.page.PageUtil;
import com.chemical.search.util.query.PropertyFilter;
import com.chemical.search.util.upload.FileBean;
import com.chemical.search.util.upload.FileInfo;
import com.chemical.search.util.upload.FileUploadUtil;
import com.chemical.search.view.BaseController;

/**
 *@className:ChemSearchController.java
 *@classDescription:
 *@author:MChen
 *@createTime:2012-4-21
 */
@Controller
@RequestMapping("/search")
public class ChemSearchController extends BaseController{
	
	Page<StructureInfo> page1 = new Page<StructureInfo>(20);
	
	Page<BaseAttribute> page2 = new Page<BaseAttribute>(20);
	
	@Autowired
	protected IBaseAttributeService baseAttributeService;
	@Autowired
	protected IStructureInfoService structureInfoService;
	
	private String qname = null;
	private String qcas = null;
	private String qformula = null;
	/**
	 * 画图进行检索
	 */
	@RequestMapping("structure_search")
	public String structureSearch(JmeDTO jme,Model model, HttpServletRequest request){
//		System.out.println(jme.getJme());
		//获取jme插件的结构分子式
		String stru = jme.getSmiles();
		System.out.println(stru+"===="+(stru == null));
		if(StringUtils.isBlank(stru)){
			return "search.jsp";
		}
		//查询条件
		PropertyFilter structure = new PropertyFilter("mol_str:LIKE_S", stru);
		List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();
		pfList.add(structure);
		//分页查询
		page1 = this.structureInfoService.searchStructure(page1,pfList);
		//生成分页标签
		page1.setPageNo(jme.getPageNo());
		//分页标签
		String tag = PageUtil.getTag(page1);
		model.addAttribute("tag", tag);
		//索引号
		model.addAttribute("index", page1.getFirst());
		
		//设置页面搜索初始值
		model.addAttribute("result", page1.getResult());
//		System.out.println(page1.getResult().size());
		model.addAttribute("tag", tag);
		return "structure_search_result.jsp";
	}
	
	@RequestMapping("molSearch")
	public String molSearch(Model model, HttpServletRequest request){

		FileInfo fileInfo=new FileInfo();
		fileInfo.setRoot(request.getSession().getServletContext().getRealPath("/"));
		fileInfo.setPath("molFileTmp");
		FileUploadUtil.upload(request, fileInfo);
		//获取文件的路径
		FileBean fileBean=fileInfo.getFileMap().get("molName");
		System.out.println(fileBean.getFieldName()+"-----"+fileBean.getFilePathName());
		/*//查询条件
		PropertyFilter structure = new PropertyFilter("mol_str:LIKE_S", stru);
		List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();
		pfList.add(structure);
		//分页查询
		page = this.structureInfoService.searchStructure(page,pfList);
		//生成分页标签
		page.setPageNo(jme.getPageNo());
		//分页标签
		String tag = PageUtil.getTag(page);
		model.addAttribute("tag", tag);
		//索引号
		model.addAttribute("index", page.getFirst());
		
		//设置页面搜索初始值
		model.addAttribute("result", page.getResult());
		System.out.println(page.getResult().size());
		model.addAttribute("tag", tag);*/
		return "head/search/list.jsp";
	}
	
	@RequestMapping("ename_search")
	public String enameSearch(JmeDTO jme,Model model, HttpServletRequest request,HttpServletResponse response){
		String type = (String)request.getParameter("type");
		//获取输入的name
		String name = jme.getName().trim();
		if(StringUtils.isBlank(name)){
			name = qname;
		}
		if(StringUtils.isBlank(name)){
			return "search_result.jsp";
		}
		PropertyFilter chemname = null;
		qcas = name;
		if(!StringUtils.isBlank(type)){
			if(type.equals("1")){
				chemname = new PropertyFilter("name_en:EQ_I", name);
			}
			else if(type.equals("2")){
				chemname = new PropertyFilter("name_en:LIKE_S", name);
			}
		}
		else {
			return null;
		}
		//查询条件
		List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();
		pfList.add(chemname);
		//设置跳转页面
		StringBuffer forwordName = new StringBuffer(this.getRoot(request)
				+ "/search/name_search.do");
		//获取分页跳转页面
		List<Condition> fragmentList=new ArrayList<Condition>();
		String forwarCondition=PageUtil.getForwardCondition(fragmentList);
		forwordName.append(forwarCondition);
		//分页查询
		page2 = this.baseAttributeService.searchAttribute(page2,pfList);
		//生成分页标签
		page2.setForwordName(forwordName.toString());
		page2.setPageNo(jme.getPageNo());
		//分页标签
		String tag = PageUtil.getTag(page2);
		model.addAttribute("tag", tag);
		//索引号
		model.addAttribute("index", page2.getFirst());
		
		//将结果传给前段页面
		model.addAttribute("result", page2.getResult());
		model.addAttribute("tag", tag);
		String currentPage=request.getRequestURI().toString()+forwarCondition+page2.getPageNo();
		request.getSession().setAttribute("currentPage",currentPage );
		return "search_result.jsp";
	} 
	
	@RequestMapping("cname_search")
	public String cnameSearch(JmeDTO jme,Model model, HttpServletRequest request,HttpServletResponse response){
		String type = (String)request.getParameter("type");
		//获取输入的name
		String name = jme.getName().trim();
		if(StringUtils.isBlank(name)){
			name = qname;
		}
		if(StringUtils.isBlank(name)){
			return "search_result.jsp";
		}
		PropertyFilter chemname = null;
		qcas = name;
		if(!StringUtils.isBlank(type)){
			if(type.equals("1")){
				chemname = new PropertyFilter("name_cn:EQ_I", name);
			}
			else if(type.equals("2")){
				chemname = new PropertyFilter("name_cn:LIKE_S", name);
			}
		}
		else {
			return null;
		}
		//查询条件
		List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();
		pfList.add(chemname);
		//设置跳转页面
		StringBuffer forwordName = new StringBuffer(this.getRoot(request)
				+ "/search/name_search.do");
		//获取分页跳转页面
		List<Condition> fragmentList=new ArrayList<Condition>();
		String forwarCondition=PageUtil.getForwardCondition(fragmentList);
		forwordName.append(forwarCondition);
		//分页查询
		page2 = this.baseAttributeService.searchAttribute(page2,pfList);
		//生成分页标签
		page2.setForwordName(forwordName.toString());
		page2.setPageNo(jme.getPageNo());
		//分页标签
		String tag = PageUtil.getTag(page2);
		model.addAttribute("tag", tag);
		//索引号
		model.addAttribute("index", page2.getFirst());
		
		//将结果传给前段页面
		model.addAttribute("result", page2.getResult());
		model.addAttribute("tag", tag);
		String currentPage=request.getRequestURI().toString()+forwarCondition+page2.getPageNo();
		request.getSession().setAttribute("currentPage",currentPage );
		return "search_result.jsp";
	}
	
	@RequestMapping("cas_search")
	public String casSearch(JmeDTO jme,Model model, HttpServletRequest request,HttpServletResponse response){
		String type = (String)request.getParameter("type");
		//获取输入的cas号
		String cas = jme.getCas().trim();
		if(StringUtils.isBlank(cas)){
			cas = qcas;
		}
		if(StringUtils.isBlank(cas)){
			return "search_result.jsp";
		}
		PropertyFilter chemcas = null;
		qcas = cas;
		if(!StringUtils.isBlank(type)){
			if(type.equals("1")){
				chemcas = new PropertyFilter("cas_rn:EQ_I", cas);
			}
			else if(type.equals("2")){
				chemcas = new PropertyFilter("cas_rn:LIKE_S", cas);
			}
		}
		else {
			return null;
		}
		//查询条件
		List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();
		pfList.add(chemcas);
		//设置跳转页面
		StringBuffer forwordName = new StringBuffer(this.getRoot(request)
				+ "/search/cas_search.do");
		//获取分页跳转页面
		List<Condition> fragmentList=new ArrayList<Condition>();
		String forwarCondition=PageUtil.getForwardCondition(fragmentList);
		forwordName.append(forwarCondition);
		//分页查询
		page2 = this.baseAttributeService.searchAttribute(page2,pfList);
		//生成分页标签
		page2.setForwordName(forwordName.toString());
		page2.setPageNo(jme.getPageNo());
		//分页标签
		String tag = PageUtil.getTag(page2);
		model.addAttribute("tag", tag);
		//索引号
		model.addAttribute("index", page2.getFirst());
		
		//将结果传给前段页面
		model.addAttribute("result", page2.getResult());
		model.addAttribute("tag", tag);
		String currentPage=request.getRequestURI().toString()+forwarCondition+page2.getPageNo();
		request.getSession().setAttribute("currentPage",currentPage );
		return "search_result.jsp";
	} 
	
	@RequestMapping("formula_search")
	public String formula_Search(JmeDTO jme,Model model, HttpServletRequest request,HttpServletResponse response){
		String type = (String)request.getParameter("type");
		//获取输入的cas号
		String formula = jme.getFormula().trim();
		System.out.println(formula+"===="+type);
		if(StringUtils.isBlank(formula)){
			formula = qformula;
		}
		if(StringUtils.isBlank(formula)){
			return "search_result.jsp";
		}
		PropertyFilter chemformula = null;
		qformula = formula;
		if(!StringUtils.isBlank(type)){
			if(type.equals("1")){
				chemformula = new PropertyFilter("molecular_formula:EQ_I", formula);
			}
			else if(type.equals("2")){
				chemformula = new PropertyFilter("molecular_formula:LIKE_S", formula);
			}
		}
		else {
			return null;
		}
		
		//查询条件
		List<PropertyFilter> pfList = new ArrayList<PropertyFilter>();
		pfList.add(chemformula);
		//设置跳转页面
		StringBuffer forwordName = new StringBuffer(this.getRoot(request)
				+ "/search/formula_search.do");
		//获取分页跳转页面
		List<Condition> fragmentList=new ArrayList<Condition>();
		String forwarCondition=PageUtil.getForwardCondition(fragmentList);
		forwordName.append(forwarCondition);
		//分页查询
		page2 = this.baseAttributeService.searchAttribute(page2,pfList);
		//生成分页标签
		page2.setForwordName(forwordName.toString());
		page2.setPageNo(jme.getPageNo());
		//分页标签
		String tag = PageUtil.getTag(page2);
		model.addAttribute("tag", tag);
		//索引号
		model.addAttribute("index", page2.getFirst());
		
		//将结果传给前段页面
		model.addAttribute("result", page2.getResult());
		model.addAttribute("tag", tag);
		String currentPage=request.getRequestURI().toString()+forwarCondition+page2.getPageNo();
		request.getSession().setAttribute("currentPage",currentPage );
		return "search_result.jsp";
	} 
	
	/**
	 * 根据CAS号获取详细信息
	 * @param model
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/info/{id}")
	public String partnerInfoById(Model model,@PathVariable("id") String id,HttpServletRequest request){
		BaseAttribute ba = this.baseAttributeService.attributeInfoByCAS(id);
		System.out.println("id============"+id);
		model.addAttribute("attributeInfo",ba);
		UserInfo user = (UserInfo)request.getSession().getAttribute("userLogin");
		if(user == null){
			System.out.println("没有登录");
			return  "info.jsp";
		}
		System.out.println("已经登录");
		return  "info1.jsp";
	}
}

